<?php

$styleon = get_option('aq_style_onoff');

// Here's the functions for the user, we haven't even started on the admin panel or functions yet.

function aq_extra_options($optionname,$stylesheet,$yesno,$id,$cname) {
	global $styleon;
	// This function makes it easier for developers to add options (styles) to Aqueous.	
	if (get_option($optionname)==$yesno || $_COOKIE['aq_'.$cname]==$yesno && $styleon=="y") {
		echo '
<link rel="stylesheet" type="text/css" media="screen" href="',bloginfo("template_url"),"/",$stylesheet,'" id="',$id,'"/>'; 
	}
	else {
		echo '
<link rel="stylesheet" type="text/css" media="screen" href="" id="',$id,'"/>'; 
	}
}
function aq_header() {
	global $styleon;
	// Lets find the options and put them into the header.
	$aq_modules = array("aq_color","aq_gloss","aq_himage","aq_reflect","aq_sidebar","aq_blogwidth");
	if ($_COOKIE['aq_color_setting']==NULL || $styleon=="n") 
		$aq_colorsrc = get_option($aq_modules[0]);
	else
		$aq_colorsrc = $_COOKIE['aq_color_setting'];
	switch ($aq_colorsrc) {
		case 2:
			echo "		<link rel=\"stylesheet\" type=\"text/css\" href=\"",bloginfo("template_url"),"/style_red.css\" id=\"colorset\" />";
			break;
		case 3:
			echo "		<link rel=\"stylesheet\" type=\"text/css\" href=\"",bloginfo("template_url"),"/style_orange.css\" id=\"colorset\" />";
			break;
		case 4:
			echo "		<link rel=\"stylesheet\" type=\"text/css\" href=\"",bloginfo("template_url"),"/style_green.css\" id=\"colorset\" />";
			break;
		case 5:
			echo "		<link rel=\"stylesheet\" type=\"text/css\" href=\"",bloginfo("template_url"),"/style_midnight.css\" id=\"colorset\" />";
			break;
		case 6:
			echo "		<link rel=\"stylesheet\" type=\"text/css\" href=\"",bloginfo("template_url"),"/style_black.css\" id=\"colorset\" />";
			break;
		default:
			echo "		<link rel=\"stylesheet\" type=\"text/css\" id=\"colorset\" />";
			break;
	}
	aq_extra_options($aq_modules[1],"gloss.css","y","l_gloss","gloss");
	aq_extra_options($aq_modules[2],"header.css","n","l_himage","himage");
	aq_extra_options($aq_modules[3],"reflect.css","n","l_reflect","reflect");
	aq_extra_options($aq_modules[4],"sidebar.css","n","l_sidebar","sidebar");
	if (isset($_COOKIE["aq_width"]) && $styleon=="y") {
		$widthnum = $_COOKIE["aq_width"];
	}
	elseif (get_option($aq_modules[5])) {
		$widthnum = (100 - get_option("aq_blogwidth")) / 2;
	}

	?>
	<style type="text/css">
		#page {
			margin: 0 <?php echo $widthnum; ?>% 0;
		}
	</style>
	<script type="text/javascript">
	function aq_wslider() {
		new Control.Slider('handle','slider', 
		{
		range:$R(0,20), 
		values:[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
		sliderValue:<?php
			$sliderv = round(get_option("aq_blogwidth"));
			if ($_COOKIE['aq_width']==NULL) 
				{ 
				echo '"',((100 - $sliderv) / 2) ,'"';
				} 
			else 
				{
				echo "getCookie('aq_width')"; 
				} ?>,
		onSlide:function(v){
			document.getElementById('widthoverlay').style.display="block",
			document.getElementById('widthoverlay').style.margin='0px '+v+'% 0px'
			new Effect.Opacity('widthoverlay', {duration:0.0, from:1.0, to:0.7});
			},
		onChange:function(v){
			document.getElementById('page').style.margin='0px '+v+'% 0px',
			setStyleCookie('aq_width',v,30),unfixSlider('widthslide'),
			new Effect.Opacity('widthoverlay', {duration:0.5, from:0.5, to:0.0});
			document.getElementById('widthoverlay').style.display='none';
			}
		}
		); 
	}
	window.onload = aq_wslider;
	</script>
	<?php
}

function check_for_aq_install() {
	if (get_option('aq_is_installed')==NULL) {
	echo '
	<div style="width:100%;background:black;color:white;font-size:1.5em;">
		<p>The owner of this blog has not fully installed Aqueous Plus yet (Admin -> Presentation -> Customize aq+ -> Install Now)</p>
	</div>';
	}
}
if ( function_exists('register_sidebars') )
	register_sidebars(1);
	
// Here's the admin functions.

function mt_add_pages() {
	// Add a new menu under Options:
	add_theme_page('Customize aq+', 'Customize aq+', 8, 'aq_customize', 'mt_theme_page');
}

function aq_install_now() {
		add_option('aq_color', '1', 'Color chosen for Aqueous Plus');
		add_option('aq_gloss', 'n', 'Gloss in the header for aqPlus?');
		add_option('aq_himage', 'y', 'Show the header image in aqPlus?');
		add_option('aq_reflect', 'y', 'Header reflection in aqPlus?');
		add_option('aq_sidebar', 'y', 'Sidebar background in aqPlus?');
		add_option('aq_blogwidth', '100', 'The width of an aqPlus blog.');
		add_option('aq_p_author', '0', 'Author on posts?');
		add_option('aq_p_date', '0', 'Display date as text?');
		add_option('aq_p_datebox', '1', 'Display date as datebox?');
		add_option('aq_p_cats', '1', 'Display categories?');
		add_option('aq_style_onoff', 'y', 'Allow users to customize the page??');
		add_option('aq_is_installed', '1.1.0', 'Proves aqPlus is installed and working correctly.');

?>
		<div class="updated">
			<p><strong><?php _e('Welcome to Aqueous Plus! Installation was successful.', 'english'); ?></strong></p>
		</div>
		<?php aqlite_admin();
}


function aq_uninstall_now() {
	delete_option('aq_color');
	delete_option('aq_gloss');
	delete_option('aq_himage');
	delete_option('aq_reflect');
	delete_option('aq_sidebar');
	delete_option('aq_blogwidth');
	delete_option('aq_p_author');
	delete_option('aq_p_date');
	delete_option('aq_p_datebox');
	delete_option('aq_p_cats');
	delete_option('aq_style_onoff');
	delete_option('aq_is_installed');
echo '
	<div class="updated">
		<p><strong>' , _e('Thanks for trying Aqueous Plus.', 'english') , '</strong></p>
	</div>';
}

function check_value($option, $value, $tag) {
	$getoption 		= get_option($option);
	if ($tag=="text") {
		return "value=\"".$getoption."\"";
	}
	elseif ($getoption==$value) {
		if ($tag=="radio") {
			return "checked=\"checked\"";
		}
		elseif ($tag=="option") {
			return "selected=\"selected\"";
		}
	}
}

function aq_post() {
	if (isset($_POST['info_update'])) {
		$color 		= $_POST["color"];
		$gloss 		= $_POST["gloss"];
		$himage 	= $_POST["himage"];
		$reflect	= $_POST["reflect"];
		$sbar		= $_POST["sidebar"];
		$width		= $_POST["blogwidth"];
		$pauthor	= $_POST["author"];
		$pdate		= $_POST["date"];
		$pdatebox	= $_POST["datebox"];
		$pcats		= $_POST["cats"];
		$styleo		= $_POST['custom'];
		if ($width > 100) {
			$width = 100;
		}
		update_option('aq_color', $color);
		update_option('aq_gloss', $gloss);
		update_option('aq_himage', $himage);
		update_option('aq_reflect', $reflect);
		update_option('aq_sidebar', $sbar);
		update_option('aq_blogwidth', $width);
		update_option('aq_p_author', $pauthor);
		update_option('aq_p_date', $pdate);
		update_option('aq_p_datebox', $pdatebox);
		update_option('aq_p_cats', $pcats);
		update_option('aq_style_onoff', $styleo);
echo '
	<div class="updated">
		<p><strong>', _e('Your theme has been changed.', 'english') , '</strong></p>
	</div>';
	} 
	elseif (isset($_POST['check_for_updates'])) {
		aq_check_for_download();
	}
};
function aq_check_for_updates() {
	if (get_option("aq_is_installed")<1.1 || get_option("aq_is_installed")=="y") {
		?>
		<h3>Update your database before continuing!</h3>
		<p>It's been detected that you have installed a new version of Aqueous Plus. Before continuing, you need to update your database to be compatible with this new version. All you have to do is click the button below - all of your Aqueous Plus settings will be saved, and you will be able to do more to customize your blog and excite your users.</p>
		<form method="post">
			<input name="update_aq" type="submit" value="Update now!" />
		</form>
<?php }
}

function aq_update_now() {
	if (get_option('aq_is_installed')=='y') {
		add_option('aq_blogwidth', '100', 'The width of an aqPlus blog.');
		add_option('aq_p_author', '0', 'Author on posts?');
		add_option('aq_p_date', '0', 'Display date as text?');
		add_option('aq_p_datebox', '1', 'Display date as datebox?');
		add_option('aq_p_cats', '1', 'Display categories?');
		add_option('aq_style_onoff', '1', 'Allow users to customize your blog??');
		update_option('aq_is_installed', '1.1.0', 'Proves aqPlus is installed and working correctly.');
		
	}
	elseif (get_option('aq_is_installed')==1.01) {
		add_option('aq_p_author', '0', 'Author on posts?');
		add_option('aq_p_date', '0', 'Display date as text?');
		add_option('aq_p_datebox', '1', 'Display date as datebox?');
		add_option('aq_p_cats', '1', 'Display categories?');
		add_option('aq_style_onoff', '1', 'Allow users to customize your blog??');
		update_option('aq_is_installed', '1.1.0');
	}
	?>
	<div class="updated">
		<p>Congratulations, Aqueous Plus has been updated (now running version 1.1.0).</p>
	</div>
	<?php
}

function aq_check_for_download() {
	$xml = simplexml_load_file("http://www.simon360.com/other/aqueous/aqueous.xml");
	if (!$xml) {
		echo "<br />The file couldn't be downloaded";
	}
	else {
		$cversionshow = get_option("aq_is_installed");
		$cversion = str_replace('.','',$cversionshow);
		$versionshow = $xml->one->latestver;
		$version = str_replace('.','',$versionshow);
		$dlhref = $xml->one->dl;
		$dlhreftgz = $xml->one->dltgz;
		if ($version > $cversion) {
			echo '<div class="updated"><p>Update '.$versionshow.' available! Your current version is '.$cversionshow.'. <a href="'.$dlhref.'">Release notes and downloads (zip and tgz).</a></p></div>';
		}
		else {
			echo '<div class="updated"><p>No new downloads available.</p></div>';
		}
	}
}


function aqlite_admin() {
	aq_post();
?>
	<style type="text/css">
		/* Because I really liked the look of K2's Options page, I decided to format this page in a similar way. 
		This mini-stylesheet was found in the source of that options page, so I'm using it here. */
		h3 {
			font: normal 1.8em Georgia;
			margin: 30px 0 0;
			color: #222;
		}

		small {
			color: #666;
		}
		
		.configstuff input[type=submit], #k2-blogornoblog, .configstuff select  {
			width: 300px;
		}

		.configstuff input[type=file] {
			width: 220px;
			background: none;
			border: none;
			padding: 0;
		}

		.configstuff input[type=checkbox] {
			margin-right: 8px;
		}

		.configstuff {
			width: 700px;
			margin: 0 auto;
			font-size: 1.1em;
		}
		
		table {
			margin: 0 auto;
			padding: 0;
			border-spacing: 0;
		}
		
		table tr td {
			height: 30px;
		}
		</style>
		<div class="configstuff">
		
		<?php aq_check_for_updates(); ?>
			<small><?php _e('Well, you found it, the aq+ customize page. This page lets you choose the diffrent options for this theme. If you want more options, and to allow your users to change it for themselves, you might want to look into using Aqueous+.','english'); ?></small>

			<table>
				<tr>
					<td colspan="2">
						<h3>Check for updates</h3>
					</td>
				</tr>
				<tr>
					<td>
						<small>It's recommended you check for updates to Aqueous Plus at least once a month, or as often as you need/want to. This script will check for new updates, and give you the direct download link to the file.</small>
					<td>
						<form method="post"><input type="submit" name="check_for_updates" value="Check now!" /></form>
					</td>
				</tr>
			</table>
		<form method="post">
			<table>
				<tr>
					<td colspan="2">
						<h3>Posts</h3>
					</td>
				</tr>
				<tr>
					<td>
						<small>These settings can change the layout of posts on your blog.</small>
					<td>
						<label for="author"><input type="checkbox" name="author" value="1" id="author" <?php echo check_value("aq_p_author", "1", "radio"); ?> />Display author</label><br />
						<label for="date"><input type="checkbox" name="date" id="date" value="1" <?php echo check_value("aq_p_date", "1", "radio"); ?> />Display date as text</label><br />
						<label for="datebox"><input type="checkbox" name="datebox" id="datebox" value="1" <?php echo check_value("aq_p_datebox", "1", "radio"); ?> />Display date as datebox</label><br />
						<label for="cats"><input type="checkbox" name="cats" id="cats" value="1" <?php echo check_value("aq_p_cats", "1", "radio"); ?> />Display categories</label>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<h3>Color</h3>
					</td>
				</tr>
				<tr>
					<td>
						<small>You can choose a color here if you want something other than the default blue to grace your entire blog.</small>
					<td>
						<select name="color">
							<option value ="1" <?php echo check_value("aq_color", 1, "option"); ?>>Blue (default)</option>
							<option value ="2" <?php echo check_value("aq_color", 2, "option"); ?>>Red</option>
							<option value ="3" <?php echo check_value("aq_color", 3, "option"); ?>>Orange</option>
							<option value ="4" <?php echo check_value("aq_color", 4, "option"); ?>>Green</option>
							<option value ="5" <?php echo check_value("aq_color", 5, "option"); ?>>Midnight</option>
							<option value ="6" <?php echo check_value("aq_color", 6, "option"); ?>>Black</option>
						</select>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<h3>Customizable by users?</h3>
					</td>
				</tr>
				<tr>
					<td><small>If you want your users to be able to customize how your page looks and feels, choose "On" here.</small> </td><td> <input type="radio" name="custom" value="y" id="ycustom" <?php echo check_value("aq_style_onoff", "y", "radio"); ?> /><label for="ycustom">On</label><input type="radio" name="custom" value="n" id="ncustom" <?php echo check_value("aq_style_onoff", "n", "radio"); ?> /><label for="ncustom">Off</label></p></td>
				</tr>
				<tr>
					<td colspan="2">
						<h3>Gloss</h3>
					</td>
				</tr>
				<tr>
					<td><small>When this is enabled, a transparent white overlay appears over the top half of the header; to enable, choose "on".</small></td><td> <input type="radio" name="gloss" value="y" id="ygloss" <?php echo check_value("aq_gloss", "y", "radio"); ?> /><label for="ygloss">On</label><input type="radio" name="gloss" value="n" id="ngloss" <?php echo check_value("aq_gloss", "n", "radio"); ?> /><label for="ngloss">Off</label></td>
				</tr>

				<tr>
					<td colspan="2">
						<h3>Header image</h3>
					</td>
				</tr>
				<tr>
					<td><small>The background that appears on the left of the header can be disabled choosing "off"</small> </td><td> <input type="radio" name="himage" value="y" id="yhimage" <?php echo check_value("aq_himage", "y", "radio"); ?> /><label for="yhimage">On</label><input type="radio" name="himage" value="n" id="nhimage" <?php echo check_value("aq_himage", "n", "radio"); ?> /><label for="nhimage">Off</label></p></td>
				</tr>
				<tr>
					<td colspan="2">
						<h3>Reflection</h3>
					</td>
				</tr>
				<tr>
					<td><small>If you want to disable the reflection under the header, you can choose "off" here.</small></td><td> <input type="radio" name="reflect" value="y" id="yreflect" <?php echo check_value("aq_reflect", "y", "radio"); ?> /><label for="yreflect">On</label><input type="radio" name="reflect" value="n" id="nreflect" <?php echo check_value("aq_reflect", "n", "radio"); ?> /><label for="nreflect">Off</label></p></td>
				</tr>
				<tr>
					<td colspan="2">
						<h3>Sidebar background</h3>
					</td>
				</tr>
				<tr>
					<td><small>The grey background on the sidebar. If you want it disabled, choose "off" here.</small></td><td> <input type="radio" name="sidebar" value="y" id="ysidebar" <?php echo check_value("aq_sidebar", "y", "radio"); ?> /><label for="ysidebar">On</label><input type="radio" name="sidebar" value="n" id="nsidebar" <?php echo check_value("aq_sidebar", "n", "radio"); ?> /><label for="nsidebar">Off</label></p></td>
				</tr>
				<tr>
					<td colspan="2">
						<h3>Blog width</h3>
					</td>
				</tr>
				<tr>
					<td><small>Changes the width of your blog, using a percentage. I recommend 60% as a minimum, but you can make it as small as you want, but no wider than 100%.</small></td><td> <input type="text" name="blogwidth" <?php echo check_value("aq_blogwidth", "y", "text"); ?> />%</p></td>
				</tr>

			</table>
			<input type="hidden" name="info_update" />
			<p class="submit">
				<input type="submit" value="Save" />
			</p>
		</form>
		<p class="aligncenter"><strong>WARNING! Clicking the button below will uninstall your Aqueous Plus theme, and visitors to your page will recieve an error message. If you want to do this, you should change to a different theme immediately, or reinstall Aqueous Plus</strong></p> <form method="post"><input type="submit" name="uninstall_aqlite" value="UNINSTALL" /></form>

<?php }
function mt_theme_page() {
	echo "<div class=\"wrap\"><h2>aq+ Options</h2>";
	if (isset($_POST["install_aqlite"])) {
		if (get_option('aq_is_installed')==NULL) {
			aq_install_now();
		}
	}
	elseif (isset($_POST["uninstall_aqlite"])) {
		aq_uninstall_now();
	}
	elseif (isset($_POST["update_aq"])) {
		aq_update_now();
	}

	elseif (get_option('aq_is_installed')==NULL) {
		echo '<p>Congratulations on your choice of Aqueous Plus! This theme has many options, but in order to use them, a few small changes have to be made to your database. This is required to use aqPlus, otherwise the theme will return an error message to all visitors to your blog. The likelyhood of anything going wrong in this process is sllim to none, so don\'t worry, clicking this button will not cause problems.</p><form method="post"><input type="submit" name="install_aqlite" value="Install aq+ now!" /></form>';
	}
	else {
		aqlite_admin();
	}
	echo "</div>";
}

add_action('admin_menu', 'mt_add_pages');
?>